package com.lemon.core.exception;

import com.lemon.core.web.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author 李猛
 * @datetime 2023/10/7 14:52
 * @description 类对象
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 权限异常，默认状态码是401
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = PermissionException.class)
    public Result<String> exception(PermissionException e) {
        log.error("权限异常");
        e.printStackTrace();
        return Result.fail(e.getCode(), e.getMessage());
    }

    /**
     * 全局异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = RuntimeException.class)
    public Result<String> exception(RuntimeException e) {
        log.error("全局异常");
        e.printStackTrace();
        return Result.fail(e.getMessage());
    }
}
